/*
 * Niiuzu
 * Copyright (C) 2011  Thierry Nowak
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.niiuzu.server.rpc.impl;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.niiuzu.server.dao.DaoFactory;
import com.niiuzu.server.dao.FeedChannelDao;
import com.niiuzu.shared.exception.ServiceRpcException;
import com.niiuzu.shared.model.FeedChannel;
import com.niiuzu.shared.model.FeedChannelGroup;
import com.niiuzu.shared.model.UserSettings;
import com.niiuzu.shared.rpc.UserService;

/**
 * Implémentation du service dédié aux utilisateurs.
 */
public class UserServiceImpl extends RemoteServiceServlet implements
		UserService {

	private static final long serialVersionUID = 3433845667124813710L;

	private static final Logger LOGGER = Logger.getLogger(UserServiceImpl.class
			.getName());

	@Override
	public UserSettings retrieveUserSettings() throws ServiceRpcException {

		LOGGER.info("Récupération des préférences de l'utilisateur");

		UserSettings settings = null;

		try {

			FeedChannelDao feedChannelDao = getFeedChannelDao();

			List<FeedChannelGroup> groupList = feedChannelDao
					.getChannelGroups();
			FeedChannelGroup[] groups = new FeedChannelGroup[groupList.size()];
			groups = groupList.toArray(groups);

			List<FeedChannel> channelList = feedChannelDao.getChannels();
			FeedChannel[] channels = new FeedChannel[channelList.size()];
			channels = channelList.toArray(channels);

			settings = new UserSettings();

			settings.setChannelGroups(groups);
			settings.setChannels(channels);

		} catch (Throwable e) {
			LOGGER.log(Level.SEVERE,
					"Erreur lors de la récupération des préférences", e);
			throw new ServiceRpcException(e);
		} finally {
			LOGGER.info("Fin de la récupération des préférences");
		}

		return settings;
	}

	/**
	 * Retourne une instance de {@link FeedChannelDao}.
	 */
	private FeedChannelDao getFeedChannelDao() {
		if (mockedFeedChannelDao != null) {
			return mockedFeedChannelDao;
		}
		return DaoFactory.getFeedChannelDao();
	}

	/* ********************************************************************** */
	/* ********************************************************************** */
	/* ********************************************************************** */

	private FeedChannelDao mockedFeedChannelDao;

	/**
	 * JUnit - Définit un mock pour {@link FeedChannelDao}.
	 */
	void setMockedFeedChannelDao(FeedChannelDao mockedFeedChannelDao) {
		this.mockedFeedChannelDao = mockedFeedChannelDao;
	}
}
